﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HotelManageSystem.Domain;
using HotelManageSystem.Domain.Entities;
using HotelManageSystem.Domain.Infrastructure;
using HotelManageSystem.Models;
using Telerik.Web.Mvc;

namespace HotelManageSystem.Controllers
{
    public class WarehouseController : Controller
    {
        //
        // GET: /Warehouse/

        private readonly IWarehouseRepository _warehouseRepository;
        private readonly IEmployeeRepository _employeeRepository;
        private readonly IUnitOfWork _unitOfWork;
        public WarehouseController (IWarehouseRepository warehouseRepository,IEmployeeRepository employeeRepository,IUnitOfWork unitOfWork)
        {
            _warehouseRepository = warehouseRepository;
            _employeeRepository = employeeRepository;
            _unitOfWork = unitOfWork;
        }
       private IEnumerable<WarehouseModel> GetWarehouseModels()
       {
           return from w in _warehouseRepository.GetAll().AsQueryable()
                  select new WarehouseModel
                             {
                                 WarehouseID = w.ID,
                                 Name = w.Name,
                                 Employee = w.Employee.Name,
                             };
       }
        public ActionResult Index()
        {
            ViewData["employees"] = _employeeRepository.GetAll();
            return View(GetWarehouseModels());
        }
        [HttpPost]
        [GridAction]
        public ActionResult SelectAjax()
        {
            ViewData["employees"] = _employeeRepository.GetAll();
            return View(new GridModel(GetWarehouseModels()));
        }
         [HttpPost]
        [GridAction]
        public ActionResult Insert(string name,int employee)
         {
             var warehouse = new Warehouse();
             warehouse.Name = name;
             warehouse.Employee = _employeeRepository.Get(x => x.ID == employee);
             if (TryUpdateModel(warehouse, null, null, new[] { "Employee" }))
             {
                 _warehouseRepository.Add(warehouse);
                 _unitOfWork.Commit();
             }
             return View(new GridModel(GetWarehouseModels()));
         }
         [HttpPost]
         [GridAction]
         public ActionResult Update(int id,int employee)
         {
             var warehouse = _warehouseRepository.Get(x => x.ID == id);
             warehouse.Employee = _employeeRepository.Get(x => x.ID == employee);
             if (TryUpdateModel(warehouse, null, null, new[] { "Employee" }))
             {
                 _warehouseRepository.Update(warehouse);
                 _unitOfWork.Commit();
             }
             return View(new GridModel(GetWarehouseModels()));
         }
         [HttpPost]
         [GridAction]
         public ActionResult Delete(int id )
         {
             var warehouse = _warehouseRepository.GetById(id);
             if(TryUpdateModel(warehouse))
             {
                 _warehouseRepository.Delete(warehouse);
                 _unitOfWork.Commit();
             }
             return View(new GridModel(GetWarehouseModels()));
         }
    }
}
